package com.amkai.aes.webservice.binary.objecthandler.supplyorg;

import java.io.Serializable;

import com.amkai.aes.aescommon.service.binary.job.supplyorg.SharedOrganizationJob;
import com.amkai.aes.aescommon.service.result.organization.SharedOrganizationResult;
import com.amkai.aes.server.organization.SharedOrganizationManager;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class SharedOrganizationHandler implements JobObjectHandler<SharedOrganizationJob, Serializable, SharedOrganizationResult> {

	@Override
	public SharedOrganizationResult process(SharedOrganizationJob job, Serializable object)	throws Exception {
		SharedOrganizationResult result = new SharedOrganizationResult();
		
		SessionManager sm = SiteManager.Start();
		sm.openSession();
		try{
			sm.openTransaction();
			try{
				SharedOrganizationManager manager = new SharedOrganizationManager(sm);
				result.setOrganization(manager.saveItem(job.getOrganization()));
				sm.commitTransaction();
			}catch(Exception exception){
				exception.printStackTrace();
				sm.rollbackTransaction();
				throw exception;
			}
		}finally{
			sm.closeSession();
		}
		
		return result;
	}
}
